package wms.handler;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import wms.entity.Function;
import wms.entity.Menu;
import wms.entity.Operator;
import wms.service.goods.GoodsService;
import wms.service.order.OrderService;
import wms.service.repo.RepoService;
import wms.service.supply.SupplyService;
import wms.util.constant.SysConstant;

@Controller
public class IndexController extends BaseController{
	
	@Resource
	private OrderService orderService;
	
	@Resource
	private SupplyService supplySevice;
	
	@Resource
	private GoodsService goodsService;
	
	@Resource
	private RepoService repoService;
	
	@RequestMapping("/index.html")
	public String getIndexPage(HttpSession session, Model model) throws Exception {
		log.debug("主页----->");
		Operator operator = (Operator)session.getAttribute(SysConstant.OPERATOR);
		if (operator == null)
			return "redirect:login.html";
		//获取菜单列表 --方法1 el动态赋值
		List<Menu> menuList = getMenuListByRole(operator.getRoleId());
		//方法2  js动态赋值
		//String jsonString = JSONObject.toJSONString(menuList);
		session.setAttribute(SysConstant.MENU_LIST, menuList);
		
		int goodsCount = goodsService.getTotalCount(null, null, null);
		int supplyCount = supplySevice.getTotalCount();
		int inOrderCount = orderService.getInOrderTotalCount(null);
		int outOrderCount = orderService.getOutOrderTotalCount(null);
		int adminCount = getOperatorService().getTotalCount();
		int repoCount = repoService.getTotalCount();
		
		model.addAttribute("goodsCount", goodsCount);
		model.addAttribute("supplyCount", supplyCount);
		model.addAttribute("inOrderCount", inOrderCount);
		model.addAttribute("outOrderCount", outOrderCount);
		model.addAttribute("adminCount", adminCount);
		model.addAttribute("repoCount", repoCount);
		return "index";
	}
	
	/**
	 * 根据角色获取菜单列表
	 * @param roleId
	 * @return
	 */
	private List<Menu> getMenuListByRole(Integer roleId) {
		List<Menu> menuList = new ArrayList<Menu>();
		//1.获取主菜单  -->redis中是否存在 存在从redis中获取 不存在从db
		//2.根据主菜单-->子菜单
		
		if (getRedisApi().keyExist(SysConstant.MENU_LIST + roleId)) {
			String redisMenuList = getRedisApi().getStringKey(SysConstant.MENU_LIST + roleId);
			menuList = JSONArray.parseArray(redisMenuList, Menu.class);
		} else {
			List<Function> mainFunList = getFunctionService().listMainFunction(roleId);
			if (null != mainFunList) {
				for (Function function : mainFunList) {
					Menu menu  = new Menu();
					menu.setMainFun(function);
					function.setRoleId(roleId);
					List<Function> sonFunList = getFunctionService().listSonFunction(function);
					if (sonFunList != null) {
						menu.setSonFun(sonFunList);
					}
					menuList.add(menu);	
				}
				//第一次登录 放入redis中
				String menuListStr = JSONObject.toJSONString(menuList);
				getRedisApi().set(SysConstant.MENU_LIST + roleId, menuListStr);
			}
		}
		return menuList;
	}
}
